goog.require('goog.dom');
goog.require('goog.fx.DragListGroup');
goog.require('goog.fx.DragListDirection');
goog.require('goog.fx.dom');
goog.require('goog.fx.easing');
goog.require('goog.ui.MenuItem');
goog.require('goog.ui.PopupMenu');
goog.require('goog.positioning.Corner');

goog.provide('tipfy.ui.ColumnManager');

tipfy.ui.ColumnManager = function(columns) {
    this.current_box = null;
    this.current_box_content = null;

    var dlg = new goog.fx.DragListGroup();
    dlg.setDragItemHandleHoverClass('box-move');
    dlg.setDraggerElClass('box-move box-opacity');
    dlg.addDragList(goog.dom.getElement('column1'), goog.fx.DragListDirection.DOWN);
    dlg.addDragList(goog.dom.getElement('column2'), goog.fx.DragListDirection.DOWN);
    dlg.setFunctionToGetHandleForDragItem(function(item) {
        return goog.dom.getElementsByTagNameAndClass('div', 'box-header', item)[0];
    });

    goog.events.listen(dlg, goog.fx.DragListGroup.EventType.DRAGSTART, function(e) {
        goog.style.setStyle(e.draggerEl, 'width', goog.style.getBounds(e.currDragItem).width + 'px');
    });
    dlg.init();


    var minimize_box = function() {
        var w = current_box_content.offsetWidth,
            h = current_box_content.offsetHeight,
            column_w = current_box_content.parentNode.offsetWidth,
            anim;

        if(h == 0) {
            anim = new goog.fx.dom.Resize(current_box_content, [w, h], [column_w, current_box_content.getAttribute('original-height')], 500, goog.fx.easing.easeOut);
        } else {
            current_box_content.setAttribute('original-height', h);
            anim = new goog.fx.dom.Resize(current_box_content, [w, h], [column_w, 0], 500, goog.fx.easing.easeOut);
        }
        goog.events.listen(anim, goog.fx.Animation.EventType.END, function() {
            current_box_content.style.width = '100%';
        });

        anim.play();
    };
    var remove_box = function() {
        var anim = new goog.fx.dom.FadeOutAndHide(current_box, 500);
        goog.events.listen(anim, goog.fx.Animation.EventType.END, function() {
            current_box.parentNode.removeChild(current_box);
        });
        anim.play();
    };

    var pm = new goog.ui.PopupMenu();
    var current_box = null;
    var current_box_content = null;
    var minimize_item = new goog.ui.MenuItem('Minimize', minimize_box);
    pm.addItem(minimize_item);
    pm.addItem(new goog.ui.MenuItem('Remove from page', remove_box));
    pm.render(document.body);

    goog.array.map(goog.dom.getElementsByTagNameAndClass('a', 'box-options'), function(el) {
        goog.events.listen(el, goog.events.EventType.CLICK, function(e) {
            current_box = el.parentNode.parentNode;
            current_box_content = goog.dom.getElementsByTagNameAndClass('div', 'box-content', current_box)[0];

            if(current_box_content.offsetHeight == 0) {
                minimize_item.setCaption('Maximize');
            } else {
                minimize_item.setCaption('Minimize');
            }

            pm.showAtElement(el, goog.positioning.Corner.BOTTOM_RIGHT, goog.positioning.Corner.TOP_RIGHT);
        });
    });

    goog.events.listen(pm, 'action', function(e) {
        e.target.getValue()();
    });
};
